<?php

namespace App\Http\Controllers\Admin;

use App\Services\ArticleCategoryService;
use App\Http\Requests\Admin\ArticleCategoryRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleCategoryController extends Controller
{

    private $articleCategoryService;

    /**
     * 注入所需要的业务类
     * @param ArticleCategoryService $articleCategoryService
     */
    public function __construct(ArticleCategoryService $articleCategoryService)
    {
        $this->articleCategoryService = $articleCategoryService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $treeArray = $this->articleCategoryService->getTreeArticleCategory();
        return view('admin.article_categories.index', ['treeArray' => $treeArray]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $treeArray = $this->articleCategoryService->getTreeArticleCategory();
        return view('admin.article_categories.create', ['treeArray' => $treeArray]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\Admin\ArticleCategoryRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(ArticleCategoryRequest $request)
    {
        $this->articleCategoryService->saveArticleCategory($request->all());
        return redirect()->route('admin.article_categories.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $articleCategory = $this->articleCategoryService->getArticleCategoryById($id);
        $treeArray = $this->articleCategoryService->getTreeArticleCategory();
        $subIds = $this->articleCategoryService->getAllSubIds($id);
        return view('admin.article_categories.edit',
            ['articleCategory' => $articleCategory, 'treeArray' => $treeArray, 'subIds' => $subIds]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\Admin\ArticleCategoryRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(ArticleCategoryRequest $request, $id)
    {
        $this->articleCategoryService->updateArticleCategoryById($id, $request->all());
        return redirect()->route('admin.article_categories.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->articleCategoryService->deleteArticleCategoryById($id);
        return redirect()->route('admin.article_categories.index');
    }

}
